<?php

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Models\User;
use App\Services\UserServices;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;


class AuthController extends WxController
{
    /**
     * 用户注册接口
     *
     * @param  Request  $request
     *
     * @return JsonResponse
     */
    public function register(Request $request)
    {
        // 获取参数  用户名username  密码password  手机号mobile  验证码code
        $username = $request->input('username');
        $password = $request->input('password');
        $mobile   = $request->input('mobile');
        $code     = $request->input('code');
        // 验证参数是否为空
        if (empty($username) || empty($password) || empty($mobile) || empty($code)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        // 验证用户是否存在
        $user = UserServices::getInstance()->getByUsername($username);
        if (!is_null($user)) {
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }
        $validator = Validator::make(['mobile' => $mobile], ['mobile' => 'regex:/^1[0-9]{10}$/']);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_INVALID_MOBILE);

        }
        $user = UserServices::getInstance()->getByMobile($mobile);
        if (!is_null($user)) {
            return $this->fail(CodeResponse::AUTH_MOBILE_REGISTERED);
        }
        //  验证验证码是否正确
        UserServices::getInstance()->checkCaptcha($mobile, $code);

        // 写入用户表
        $user                  = new User();
        $user->username        = $username;
        $user->password        = Hash::make($password);
        $user->mobile          = $mobile;
        $user->avatar          = "https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64";
        $user->nickname        = $username;
        $user->last_login_time = Carbon::now()->toDateTimeString();
        $user->last_login_ip   = $request->getClientIp();
        $user->save();
        // todo 新用户发送注册优惠劵
        // todo 返回用户信息和token
        return $this->success([
            'token'    => '',
            'userInfo' => [
                'nickname'  => $username,
                'avatarUrl' => $user->avatar,
            ],
        ]);
    }

    //获取手机验证码
    public function regCaptcha(Request $request)
    {
        //  获取手机号
        $mobile = $request->input('mobile');
        //  验证手机号是否合法
        if (empty($mobile)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $validator = Validator::make(['mobile' => $mobile], ['mobile' => 'regex:/^1[0-9]{10}$/']);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_INVALID_MOBILE);
        }
        //  验证手机号是否已被注册
        $user = UserServices::getInstance()->getByMobile($mobile);
        if (!is_null($user)) {
            return $this->fail(CodeResponse::AUTH_MOBILE_REGISTERED);
        }

        //  防刷验证,一分钟内只能请求一次，当天最多请求十次
        $lock = Cache::add("register_captcha_lock_$mobile", 1, 60);
        if (!$lock) {
            return $this->fail(CodeResponse::AUTH_CAPTCHA_FREQUENCY);
        }
        $isPass = UserServices::getInstance()->checkMobileSendCaptchaCount($mobile);
        if (!$isPass) {
            return $this->fail(CodeResponse::AUTH_CAPTCHA_FREQUENCY,'验证码当天发送不能超过10次');
        }
        //  随机生成6位验证码
        //  保存手机号和验证码的关系
        $code = UserServices::getInstance()->setCaptcha($mobile);
        $data = [$code,1];
        //  发送短信
        UserServices::getInstance()->sendCaptchaMsg($mobile,$data);
        return $this->success();
    }
}





























